﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Common;
using Think.Lenovo.Com.Models.Questionnaire;

namespace Think.Lenovo.Com.DataAccess.Questionnaire
{
    public class Question
    {
        /// <summary>
        /// 添加一个问卷
        /// </summary>
        /// <param name="model">问卷实体对象</param>
        /// <returns>问卷id</returns>
        public long AddQuestion(QuestionModel model)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append(" tbl_questionn (QuestionSeq,QuestionContent)")
                .Append(" values (@QuestionSeq,@QuestionContent)");

            MySqlParameter[] ps = new MySqlParameter[]{ 
                   new MySqlParameter("@QuestionSeq",MySqlDbType.VarChar,56),
                    new MySqlParameter("@QuestionContent",MySqlDbType.VarChar,256)
                };
            ps[0].Value = model.QuestionSeq;
            ps[1].Value = model.QuestionContent;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;
        }
        /// <summary>
        /// 修改一个问卷
        /// </summary>
        /// <param name="model">问卷实体对象</param>
        /// <returns>受影响的行数</returns>
        public int UpdateQuestion(QuestionModel model)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Update ")
                .Append("  tbl_question Set  QuestionSeq=@QuestionSeq,QuestionContent=@QuestionContent")
                .Append(" Where Id=@Id");

            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@QuestionContent",MySqlDbType.VarChar,256),
                    new MySqlParameter("@Id",MySqlDbType.Int32),
                    new MySqlParameter("@QuestionSeq",MySqlDbType.VarChar,56)
                };

            ps[0].Value = model.QuestionContent;
            ps[1].Value = model.Id;
            int affect = MysqlHelper.Update(conn, sql.ToString(), ps);
            return affect;

        }
        /// <summary>
        /// 得到一个问卷实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public QuestionModel GetQuestion(int id)
        {
            QuestionModel model = new QuestionModel();
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(56);
            sql.Append("Select * From tbl_question Where Id=" + id);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DateTime now = DateTime.Now;
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.QuestionSeq = Convert.ToString(ds.Tables[0].Rows[0]["QuestionSeq"]);
                        model.QuestionContent = Convert.ToString(ds.Tables[0].Rows[0]["QuestionContent"]);
                    }
                }
            }
            return model;
        }
        /// <summary>
        /// 获得pageSize条数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示数据数量</param>
        /// <returns></returns>
        public LinkedList<QuestionModel> GetQuestions(int pageIndex, int pageSize)
        {
            LinkedList<QuestionModel> list = new LinkedList<QuestionModel>();
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(56);
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }
            sql.Append("Select * From tbl_question order by Id desc limit ")
                .Append((pageIndex - 1) * pageSize)
                .Append(",")
                .Append(pageSize);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            QuestionModel model = new QuestionModel();
                            model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                            model.QuestionSeq = Convert.ToString(ds.Tables[0].Rows[0]["QuestionSeq"]);
                            model.QuestionContent = Convert.ToString(ds.Tables[0].Rows[0]["QuestionContent"]);
                            list.AddLast(model);
                        }
                    }
                }
            }
            return list;
        }
    }
}
